package org.jeecg.modules.demo.znnqjc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xkcoding.http.HttpUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.znnqjc.entity.Dklist;
import org.jeecg.modules.demo.znnqjc.mapper.DklistMapper;
import org.jeecg.modules.demo.znnqjc.service.IDklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 地块列表
 * @Author: jeecg-boot
 * @Date:   2023-04-14
 * @Version: V1.0
 */
@Service
public class DklistServiceImpl extends ServiceImpl<DklistMapper, Dklist> implements IDklistService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DklistMapper dklistMapper;

    //获取token
    @Override
    public String getToken() {
        //先获取redis中对应键的值
        String token = (String) redisTemplate.opsForValue().get("nstoken");
        //如果redis中为空
        if (!StringUtils.isEmpty(token)) {
            return token;
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("key", "b30f78856c7811eda35b00163e0db388");
            map.put("secret", "483a993a05390b10ad9c6d9b8a010468");

            String s = HttpUtil.get("http://121.89.199.196/farming/apitoken/getToken", map, false);
            JSONObject jsonObject = JSONObject.parseObject(s);
            System.out.println(jsonObject);
            String string = jsonObject.getJSONObject("data").getString("token");
            redisTemplate.opsForValue().set("nstoken", string, 60, TimeUnit.MINUTES);
            return string;

        }
    }

    //获取地块列表数据
    @Override
    public String getdklist(){
        getToken();
        try {
            String token = (String) redisTemplate.opsForValue().get("nstoken");
            System.out.println(token);
//            String param1 = "param1_value";
//            String param2 = "param2_value";
            String url = "http://121.89.199.196/farming/openapi/getMassData";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("accessToken",token);
            int responseCode = con.getResponseCode();
            System.out.println("Response Code : " + responseCode);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            JSONObject jsonObject= JSON.parseObject(String.valueOf(response));
            JSONArray jsonArray=new JSONArray(jsonObject.getJSONArray("data"));
            System.out.println(jsonArray.size());
            for (int i = 0;i<=jsonArray.size();i++){
                JSONObject jsonObject2= jsonArray.getJSONObject(i);
                System.out.println(jsonObject2);
                Dklist dklist=new Dklist();
                dklist.setMj(jsonObject2.getString("area"));
                dklist.setDkmc(jsonObject2.getString("fullname"));
                dklist.setDkbm(jsonObject2.getString("areaid"));
                dklistMapper.insert(dklist);
            }

        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
        return "ok";
    }

    @Override
    public Result getdklista(){
        QueryWrapper<Dklist> queryWrapper=new QueryWrapper<>();
        List<Dklist> list=dklistMapper.selectList(queryWrapper);
        return Result.OK(list);
    }

}
